package com.leetcode.sorts.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author: WuPeng
 * @Date: 2021/6/3 7:42 下午
 *
 * 101.对称二叉树
 */
public class SymmetricTree_101 {

    public boolean isSymmetric(TreeNode root){
        if (root == null) return false;
        return compare(root.left, root.right);
    }

    private boolean compare(TreeNode left, TreeNode right) {
        if (left == null && right == null) return true;
        else if (left != null && right == null) return false;
        else if (left == null && right != null) return false;
        else if (left.val != right.val) return false;
        return compare(left.left, right.right) && compare(left.right, right.left);
    }
//[1,2,2,3,4,4,3]
    public boolean isSymmetric2(TreeNode root){
        if (root == null) return false;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        while (!queue.isEmpty()){
            TreeNode left = queue.poll();
            TreeNode right= queue.poll();
            if (left==null && right==null) continue;
            if ((left == null && right != null) || (left != null && right == null) || (left.val != right.val)) return false;

            queue.offer(left.left);
            queue.offer(right.right);

            queue.offer(left.right);
            queue.offer(right.left);

        }
        return true;
    }


    public static void main(String[] args) {

        Queue<Integer> queue = new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);

        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.peek());

    }

}
